﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Net.Mail;
using System.Net;
using Hired.Classes;
using System.Globalization;

namespace Hired
{
    public partial class empregister : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // checking cblAccountSettings
            foreach (ListItem li in cblAccountSettings.Items)
            {
                li.Selected = true;
            }
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            if (IsValid)
            {
                CompanyAccount account = new CompanyAccount();
                Company co = new Company();
                EmployerPassport ep = new EmployerPassport();

                #region Converting field values to code attributes

                //attributes(account)
                account.Activated = false;
                account.Email = txtEmail.Text;
                account.SecurityQuestion = ddlSecurityQuestion.Text;
                account.SecurityAnswer = txtSecurityAnswer.Text;
                account.PrivacySettings = 0;
                account.Username = txtUsername.Text;
                account.Password = Encryption.md5Encrypt(txtPassword.Text);
                if (cblAccountSettings.Items[0].Selected)
                {
                    account.AllowMessages = true;
                }
                else
                {
                    account.AllowMessages = false;
                }
                if (cblAccountSettings.Items[1].Selected)
                {
                    account.AllowNotifications = true;
                }
                else
                {
                    account.AllowNotifications = false;
                }

                //attributes(company)
                co.CompanyName = txtCompanyName.Text;
                co.Address = txtAddress1.Text + txtAddress2.Text + txtAddress3.Text;
                co.PostalCode = txtPostalCode.Text;
                co.ContactNumber = txtContactNum.Text;
                co.WebsiteURL = txtWebsiteUrl.Text;
                co.ImageAddress = "images/ProfilePic.png";

                //dateFounded
                string stringDateFounded = txtDateFounded.Text;
                CultureInfo ci = new CultureInfo("es-ES");
                co.DateFounded = DateTime.Parse(stringDateFounded, ci);

                ////VALIDATION

                //checkPostalCode
                if (!checkPostalCode())
                {
                    RequiredFieldPostalCode.Text = "Postal code must be 6 digits";
                }
                else
                {
                    co.PostalCode = txtPostalCode.Text;
                }

                #endregion

                //AddtoEmployerPassport
                if (DBCompanyAccount.AddCompanyAccount(account, co))
                {
                    Send(txtEmail.Text);
                    Response.Redirect("registersuccess.aspx");
                }
            }
        }



        //METHOD

        //CheckUsername
        public bool checkUsername()
        {
            if (DBAccount.RetrieveAccountByUsername(txtUsername.Text) == null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //postal code
        public Boolean checkPostalCode()
        {
            if (txtPostalCode.Text.Length != 6)
            {
                return false;
            }

            else
            {
                return true;
            }
        }

        //Email
        public void Send(string recipient)
        {
            string from = ConfigurationManager.AppSettings["Username"];
            string server = ConfigurationManager.AppSettings["MailServer"];
            string password = ConfigurationManager.AppSettings["password"];


            MailMessage mail = new MailMessage(from, recipient);
            SmtpClient smtp = new SmtpClient(server, 587);
            NetworkCredential nc = new NetworkCredential(from, password);

            mail.Subject = "Welcome to Hired. Please activate your account.";
            mail.IsBodyHtml = true;
            mail.Body = "Thank you for registering with Hired <br /><br /> Your username:" + txtUsername.Text + "<br />Password:" + txtPassword.Text
                + "<br /><br />You can activate your account @ http://hired.com/activate.aspx?username=" + txtUsername.Text + "@activationID=" + Encryption.md5Encrypt(txtUsername.Text)
                + "<br /><br />If you have trouble activating your account. Please go to <a href=\"http://hired.com/activate.aspx\">here</a>"
                    + "<br />Your activation ID:" + Encryption.md5Encrypt(txtUsername.Text);

            smtp.EnableSsl = true;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = nc;

            try
            {
                smtp.Send(mail);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        protected void CustomUsername_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = checkUsername();
        }

        protected void CustomEmail_ServerValidate(object source, ServerValidateEventArgs args)
        {
            object account = DBAccount.RetrieveAccountByEmail(txtEmail.Text);
            string accountType = DBAccount.RetrieveAccountTypeByAccount(account);
            if (DBAccount.RetrieveAccountByEmail(txtEmail.Text) == null)
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }

        protected void CustomDateFounded_ServerValidate(object source, ServerValidateEventArgs args)
        {
            string stringDateFounded = txtDateFounded.Text;
            CultureInfo ci = new CultureInfo("es-ES");
            DateTime inputDOB = DateTime.Parse(stringDateFounded, ci);

            if (inputDOB < DateTime.Now)
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }
    }
}